package com.ee.chat.admin.controller.login;

import com.ee.chat.admin.service.ILoginService;
import com.ee.chat.common.bean.Result;
import com.ndhutu.ee.util.captcha.Captcha;
import com.ndhutu.ee.util.captcha.GifCaptcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    private Logger log = LoggerFactory.getLogger(LoginController.class);

    @Resource
    ILoginService loginService;

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        if(session.getAttribute("adminUser") != null){
            return "redirect:/toDashboard";
        }

        return "login";
    }

    /**
     * 登录前生产验证码
     * @Author Shane
     * @Date 2019/7/1115:51
     * @param
     * @return
     **/
    @RequestMapping("beforeLogin")
    public void beforeLogin(HttpServletRequest request, HttpServletResponse response){
        try {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/gif");

            //生成验证码
            Captcha captcha = new GifCaptcha(146, 42, 4);
            ServletOutputStream outputStream = response.getOutputStream();
            captcha.out(outputStream);
            outputStream.flush();

            //存入request
            request.setAttribute("code", captcha.text().toLowerCase());
        } catch (Exception e){
            log.error("【生产验证码错误】", e);
        }
    }

    /**
     * 登录
     * @Author Shane
     * @Date 2019/7/1116:21
     * @param
     * @return
     **/
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(HttpServletRequest request, @RequestParam String username, @RequestParam String password){
        try {
            return loginService.login(request, username, password);
        } catch (Exception e){
            log.error("【登录错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 注销
     * @Author Shane
     * @Date 2019/7/1116:22
     * @param
     * @return
     **/
    @RequestMapping("logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        session.removeAttribute("adminUser");
        session.removeAttribute("permissions");
        session.removeAttribute("userPermissions");
        return "login";
    }

    /**
     * 修改密码
     * @Author Shane
     * @Date 2019/9/1020:35
     * @param
     * @return
     **/
    @RequestMapping("updPwd")
    @ResponseBody
    public Result updPwd(HttpServletRequest request, @RequestParam String oldPwd, @RequestParam String newPwd){
        try {
            return loginService.updPwd(request, oldPwd, newPwd);
        } catch (Exception e){
            log.error("【修改密码错误】", e);
        }
        return Result.returnFailure();
    }
}
